package com.atguigu.gmall.car.controller;


import com.alibaba.nacos.common.utils.StringUtils;
import com.atguigu.gmall.car.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: CartApiController
 * Package: com.atguigu.gmall.car.controller
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-08-31 11:41
 * @Version 1.0
 */

@RestController
@RequestMapping("/api/cart")
public class CartApiController {


    @Autowired
    private CartService cartService;
    /**
     * 添加购物车
     * @param skuId
     * @param skuNum
     * @param request
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request){
        //  从请求头中获取数据
        String userId = AuthContextHolder.getUserId(request);
        //  判断userId 为空
        if (StringUtils.isEmpty(userId)){
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法
        this.cartService.addToCart(skuId,skuNum,userId);
        //  默认返回
        return Result.ok();
    }

    /**
     * 查看谁的购物车列表
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //  从请求头中获取数据
        String userId = AuthContextHolder.getUserId(request);
        //  获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        //  调用服务层方法
        List<CartInfo> cartInfoList = this.cartService.cartList(userId,userTempId);
        return Result.ok(cartInfoList);
    }

    /**
     * 修改选中状态
     * @param skuId
     * @param isChecked
     * @param request
     * @return
     */
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  判断是否有用户Id
        if (org.springframework.util.StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用service
        this.cartService.checkCart(skuId,isChecked,userId);
        //  返回
        return Result.ok();
    }

    /**
     * 全选
     * @param isChecked
     * @param request
     * @return
     */

    @GetMapping("/allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable Integer isChecked,
                               HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  判断是否有用户Id
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用service
        this.cartService.allCheckCart(isChecked,userId);
        //  返回
        return Result.ok();
    }

    /**
     * 删除购物车
     * @param skuId
     * @param request
     * @return
     */
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  判断是否有用户Id
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  删除购物车
        this.cartService.deleteCart(skuId,userId);

        return Result.ok();
    }

    /**
     * 清空购物车
     * @param request
     * @return
     */
    @GetMapping("clearCart")
    public Result clearCart(HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  判断是否有用户Id
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  删除购物车
        this.cartService.clearCart(userId);

        return Result.ok();
    }

    /**
     * 获取送货清单
     * @param userId
     * @return
     */

    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){
        //  调用服务层方法。
        return this.cartService.getCartCheckedList(userId);
    }
}